from google.appengine.ext import db
from google.appengine.api import memcache

class BaseRepository(object):

    def getByKey(self, entitykey, entitytype):
        if (isinstance(entitykey, db.Key)):
            cachedentity = memcache.get(str(entitykey))
            if (cachedentity is not None):
                return cachedentity
            else:
                cachedentity = entitytype.get(entitykey)
                memcache.add(str(cachedentity.key), cachedentity, 3600)
                return cachedentity

    def save(self, entity, entitytype):
        if (isinstance(entity, entitytype)):
            entity.put()
            memcache.set(str(entity.key), entity, 3600)
            return entity.is_saved()
        else:
            return False

    def delete(self, oldentity, entitytype):
        if (isinstance(oldentity, entitytype)):
            memcache.delete(str(oldentity.key))
            oldentity.delete()






